package com.saer.client.window;

import java.util.LinkedHashMap;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.saer.client.SaerEntryPoint;
import com.saer.client.constants.ClientConstants;
import com.saer.client.dto.network.MsgDto;
import com.saer.client.tab.diagnostico.DiagnosticoDistanciaTab;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.HLayout;

public class ReplyWindow extends Window {

	private Button sendButton = new Button();
	private Button cancelButton = new Button();
	private DiagnosticoDistanciaTab sourceTab = null;
	private TextAreaItem textArea = new TextAreaItem();
	private SelectItem accion = new SelectItem();

	public ReplyWindow(DiagnosticoDistanciaTab vSourceTab) {

		this.sourceTab = vSourceTab;
		
		setTitle("Respuesta");
		setWidth(270);
		setHeight(215);
		setLayoutAlign(Alignment.CENTER);
		setShowMinimizeButton(false);
		setShowCloseButton(false);
		setIsModal(true);
		setShowModalMask(true);
		centerInPage();

		sendButton.setWidth(100);
		sendButton.setHeight(25);
		sendButton.setTitle("Enviar");
		sendButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SaerEntryPoint.getRemotewebservice().sendToChacra(
						sourceTab.getVisitaDto().getVisitaId(),
						sourceTab.getVisitaDto().getCap(),
						accion.getValueAsString() + " - " + textArea.getValueAsString(),
						new ResponderCallBack());
				destroy();
			}
		});
		
		cancelButton.setWidth(100);
		cancelButton.setHeight(25);
		cancelButton.setTitle("Cancelar");
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				destroy();
			}
		});
		
		HLayout buttonsLayout = new HLayout(30);
		buttonsLayout.setAlign(Alignment.CENTER);
		buttonsLayout.setLayoutAlign(VerticalAlignment.CENTER);
		buttonsLayout.setWidth(120);
		buttonsLayout.setHeight(30);
		buttonsLayout.addMember(sendButton);
		buttonsLayout.addMember(cancelButton);


		textArea.setShowTitle(false);
		textArea.setColSpan(2);
		textArea.setWidth("100%");
		accion.setTitle("Accion");
		LinkedHashMap<String, String> acciones = new LinkedHashMap<String, String>();
		acciones.put("TRASLADO", "Solicitud de traslado");
		acciones.put("NO_TRASLADO", "No se translada");
		accion.setValueMap(acciones);
		accion.setValue("TRASLADO");
		accion.setWidth(200);
		accion.setTitleAlign(Alignment.LEFT);
		
		DynamicForm form = new  DynamicForm();
		form.setNumCols(2);
		form.setPadding(5);
		form.setCellPadding(5);
		form.setAlign(Alignment.LEFT);
		form.setFields(textArea,accion);
		
		addItem(form);
		addItem(buttonsLayout);
		
		show();

	}
	
	private class ResponderCallBack implements AsyncCallback<MsgDto> {

		public void onFailure(Throwable arg0) {
			sourceTab.showErrorIcon(arg0.getMessage(), this.getClass().getName());
		}

		public void onSuccess(MsgDto result) {
			if (!validate(result)) {
				sourceTab.showErrorIcon("Error desconocido (mensaje corrupto)", this.getClass().getName());
			} else {
				if (result.getAction() == ClientConstants.OP_SEND_MSG_OK) {
					SC.say("La respuesta se envio correctamente");
					SaerEntryPoint.getRemotewebservice().updateStatusVisita(
							sourceTab.getVisitaDto().getVisitaId(),
							ClientConstants.VISITA_CONTESTADA,
							new AsyncCallback<Void>() {
								public void onFailure(Throwable caught) {}
								public void onSuccess(Void result) {}
							});
					
					sourceTab.getVisitaTab().getVisitaGrid().cambiarEstadoVisita(
							sourceTab.getVisitaDto().getVisitaId(),
							ClientConstants.VISITA_CONTESTADA);
					
					sourceTab.getRespondButton().setDisabled(true);
					
					return;
				} else if (result.getAction() == ClientConstants.OP_SEND_MSG_ERROR) {
					sourceTab.showErrorIcon(result.getData(), this.getClass().getName());
				} else {
					sourceTab.showErrorIcon("Operacion Desconocida", this.getClass().getName());
				}
			}
		}

		private boolean validate(MsgDto result) {
			if (result != null) {
				return true;
			}
			return false;
		}
	}
	
}
